<?php
/**
 * Created by PhpStorm.
 * User: xiaomeng
 * Date: 17-9-18
 * Time: 下午4:20
 */
namespace swf\common;


class Bet
{
    private $appKey = "higame";
    private $appSecret = "123456";
    private $gameKey = "901";
    private $gameSetId = "10005";// 游戏局流水号
    private $url = "http://qa.hi-api.8win.com/biz";
    private $ver = "v1";
    private $requestId = '';
    private $curl = null;
    public $errMsg;//错误信息
    public $orderId;

    public function __construct()
    {
        $this->curl = new Curl();
    }



    //token认证
    public function token($userOpenId,$token)
    {
        $url = $this->genUrl('tokenVerify');
        $reqData = $this->reqData(["userOpenId"=>$userOpenId,"token"=>$token]);
        $res = $this->curl->post($url,$reqData);
        return $this->decodeData($res);

    }

    //账户查询
    public function balance($userId)
    {
        $url = $this->genUrl('balance');
        $reqData = $this->reqData(["userId"=>$userId]);
        $res = $this->curl->post($url,$reqData);
        return $this->decodeData($res);
    }

    //游戏投注
    public function bet($gameSetId,$orderId,$userId,$amount,$operationTime,$description)
    {
        $url = $this->genUrl('bet');
        $_data = [
            'gameKey'=>$this->gameKey,// 游戏 key
            'gameSetId'=>$gameSetId,
            'orderId'=>$orderId,
            'userId'=>$userId,
            'amount'=>$amount,
            'operationTime'=>$operationTime,
            'description'=>$description
        ];

        $reqData = $this->reqData($_data);
        $res = $this->curl->post($url,$reqData);
        return $this->decodeData($res);
    }

    //游戏派奖
    public function prize($gameSetId,$orderId,$userId,$amount,$operationTime,$description)
    {
        $url = $this->genUrl('prize');
        $_data = [
            'gameKey'=>$this->gameKey,// 游戏 key
            'gameSetId'=>$gameSetId,
            'orderId'=>$orderId,
            'userId'=>$userId,
            'amount'=>$amount,
            'operationTime'=>$operationTime,
            'description'=>$description
        ];
        $reqData = $this->reqData($_data);
        $res = $this->curl->post($url,$reqData);
        return $this->decodeData($res);
    }

    //投注撤销
    public function betRefund($gameSetId,$orderId,$operationTime,$description)
    {
        $url = $this->genUrl('betRefund');
        $_data = [
            'gameKey'=>$this->gameKey,// 游戏 key
            'gameSetId'=>$gameSetId,
            'orderId'=>$orderId,
            'operationTime'=>$operationTime,
            'description'=>$description
        ];
        $reqData = $this->reqData($_data);
        $res = $this->curl->post($url,$reqData);
        return $this->decodeData($res);

    }

    //订单查询 1.投注 2.派奖 3.撤销
    public function find($orderId,$type = 1)
    {
        $url = $this->genUrl('find');
        $_data = [
            'gameKey'=>$this->gameKey,// 游戏 key
            'type'=>$type,
            'orderId'=>$orderId,
        ];
        $reqData = $this->reqData($_data);
        $res = $this->curl->post($url,$reqData);
        return $this->decodeData($res);

    }

    //生成url
    private function genUrl($cmd)
    {
        return $this->url.'/'.$this->ver.'/'.$cmd;
    }

    //生成requestID
    private function genRequestId()
    {
        return Utils::create_uuid();
    }

    //生成签名
    private function sign($data)
    {
        if(empty($data))
        {
            return $data;
        }
        return sha1($this->appKey.$this->appSecret.$this->requestId.$data);
    }

    //请求数据
    private function reqData($data)
    {
        $this->requestId = $this->genRequestId();
        $_data = [
            "appKey"=>$this->appKey,
            "requestId"=>$this->requestId,
            "data"=>json_encode($data),
            "sign"=>$this->sign(json_encode($data)),
        ];
        return json_encode($_data);
    }

    /**
     * 解析数据
     * @param $data
     * @return mixed
     */
    private function decodeData($data)
    {
        $res = json_decode($data,true);
        if(isset($res['code']) && $res['code'] == 0)
        {

        return json_decode($res['data'],true);
        }
        return false;
    }
}
